#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <chrono>

int main(int argc, char **argv)
{
    {
        uint16_t crc = 0;
        auto ts = std::chrono::system_clock::now();
        for (int j = 0; j < 100000; j++)
        {
            int *v = (int*)malloc(1024*sizeof(int));
            memset(v, 0, 4096);
            crc += v[1];
            for(int i = 0; i < 1024; i++)
            {
                v[i] = i;
            }
            free(v);
        }
        auto te = std::chrono::system_clock::now();
        std::cout << (double)(te - ts).count() / 1000000 << " ms\t" << (te - ts).count() << " count\t" << crc << std::endl;
    }
    {
        uint16_t crc = 0;
        auto ts = std::chrono::system_clock::now();
        for (int j = 0; j < 100000; j++)
        {
            int *v = (int*)malloc(4096);
            memset(v, 0, 4096);
            crc += v[1];
            for(int i = 0; i < 1024; i++)
            {
                v[i] = i;
            }
            free(v);
        }
        auto te = std::chrono::system_clock::now();
        std::cout << (double)(te - ts).count() / 1000000 << " ms\t" << (te - ts).count() << " count\t" << crc << std::endl;
    }
    {
        uint16_t crc = 0;
        auto ts = std::chrono::system_clock::now();
        for (int j = 0; j < 100000; j++)
        {
            int *v = new int[1024];
            memset(v, 0, 4096);
            crc += v[1];
            for(int i = 0; i < 1024; i++)
            {
                v[i] = i;
            }
            delete[] v;
        }
        auto te = std::chrono::system_clock::now();
        std::cout << (double)(te - ts).count() / 1000000 << " ms\t" << (te - ts).count() << " count\t" << crc << std::endl;
    }
    {
        uint16_t crc = 0;
        auto ts = std::chrono::system_clock::now();
        for (int j = 0; j < 100000; j++)
        {
            std::vector<int> v(1024);
            crc += v[1];
            for(int i = 0; i < 1024; i++)
            {
                v[i] = i;
            }
        }
        auto te = std::chrono::system_clock::now();
        std::cout << (double)(te - ts).count() / 1000000 << " ms\t" << (te - ts).count() << " count\t" << crc << std::endl;
    }
    {
        uint16_t crc = 0;
        auto ts = std::chrono::system_clock::now();
        for (int j = 0; j < 100000; j++)
        {
            std::vector<int> v;
            v.resize(1024);
            crc += v[1];
            for(int i = 0; i < 1024; i++)
            {
                v[i] = i;
            }
        }
        auto te = std::chrono::system_clock::now();
        std::cout << (double)(te - ts).count() / 1000000 << " ms\t" << (te - ts).count() << " count\t" << crc << std::endl;
    }
    {
        uint16_t crc = 0;
        auto ts = std::chrono::system_clock::now();
        for (int j = 0; j < 100000; j++)
        {
            std::vector<int> v;
            v.reserve(1024);
            for(int i = 0; i < 1024; i++)
            {
                v.push_back(i);
            }
            crc += v[1];
        }
        auto te = std::chrono::system_clock::now();
        std::cout << (double)(te - ts).count() / 1000000 << " ms\t" << (te - ts).count() << " count\t" << crc << std::endl;
    }
    {
        uint16_t crc = 0;
        auto ts = std::chrono::system_clock::now();
        for (int j = 0; j < 100000; j++)
        {
            std::vector<std::string> v(1024);

            for(int i = 0; i < 1024; i++)
            {
                v[i] = std::to_string(i);
            }
            crc += v[1][0];
        }
        auto te = std::chrono::system_clock::now();
        std::cout << (double)(te - ts).count() / 1000000 << " ms\t" << (te - ts).count() << " count\t" << crc << std::endl;
    }
    {
        uint16_t crc = 0;
        auto ts = std::chrono::system_clock::now();
        for (int j = 0; j < 100000; j++)
        {
            std::vector<std::string> v;
            v.resize(1024);

            for(int i = 0; i < 1024; i++)
            {
                v[i] = std::to_string(i);
            }
            crc += v[1][0];
        }
        auto te = std::chrono::system_clock::now();
        std::cout << (double)(te - ts).count() / 1000000 << " ms\t" << (te - ts).count() << " count\t" << crc << std::endl;
    }
    {
        uint16_t crc = 0;
        auto ts = std::chrono::system_clock::now();
        for (int j = 0; j < 100000; j++)
        {
            std::vector<std::string> v;
            v.reserve(1024);
            for(int i = 0; i < 1024; i++)
            {
                v.push_back(std::to_string(i));
            }
            crc += v[1][0];
        }
        auto te = std::chrono::system_clock::now();
        std::cout << (double)(te - ts).count() / 1000000 << " ms\t" << (te - ts).count() << " count\t" << crc << std::endl;
    }
    return 0;
}

// Ubuntu20.04.6 gcc编译
// Debug： malloc ~= new < vector(resize) ~< vector(n) < vector(reserve)
// Release： malloc < new < vector(n) < vector(resize) < vector(reserve)
// 总体来说越原始效率越高，标准c效率高于c++，c++中new高于vector容器
//
// Debug:
// xtwh@u2004:~/progs/Crc/crc16_mcrf4xx/build$ /home/xtwh/progs/Crc/crc16_mcrf4xx/build/crc16_mcrf4xx
// 320.192 ms      320191660 count 0
// 265.646 ms      265646482 count 0
// 287.114 ms      287114112 count 0
// 532.701 ms      532700731 count 0
// 517.51 ms       517509833 count 0
// 900.73 ms       900730397 count 34464
// 9951.19 ms      9951191082 count        50336
// 10174.1 ms      10174103979 count       50336
// 11371.7 ms      11371651097 count       50336
// xtwh@u2004:~/progs/Crc/crc16_mcrf4xx/build$ /home/xtwh/progs/Crc/crc16_mcrf4xx/build/crc16_mcrf4xx
// 313.83 ms       313829538 count 0
// 289.023 ms      289022622 count 0
// 288.833 ms      288832777 count 0
// 516.929 ms      516929388 count 0
// 508.847 ms      508846829 count 0
// 901.491 ms      901490917 count 34464
// 9995.65 ms      9995645336 count        50336
// 10400.1 ms      10400146737 count       50336
// 11057.6 ms      11057587680 count       50336
// xtwh@u2004:~/progs/Crc/crc16_mcrf4xx/build$ /home/xtwh/progs/Crc/crc16_mcrf4xx/build/crc16_mcrf4xx
// 287.329 ms      287329290 count 0
// 284.338 ms      284338235 count 0
// 272.634 ms      272633797 count 0
// 530.793 ms      530792710 count 0
// 523.049 ms      523048927 count 0
// 921.442 ms      921441638 count 34464
// 9946.4 ms       9946397869 count        50336
// 10372 ms        10372009761 count       50336
// 12696.1 ms      12696063823 count       50336

// 
// Release:
// xtwh@u2004:~/progs/Crc/crc16_mcrf4xx/build$ /home/xtwh/progs/Crc/crc16_mcrf4xx/build/crc16_mcrf4xx
// 14.3424 ms      14342375 count  0
// 17.6948 ms      17694805 count  0
// 21.2923 ms      21292323 count  0
// 18.9999 ms      18999918 count  0
// 21.4199 ms      21419935 count  0
// 44.0729 ms      44072853 count  34464
// 5958.15 ms      5958154077 count        50336
// 6160.2 ms       6160203394 count        50336
// 5990.3 ms       5990298171 count        50336
// xtwh@u2004:~/progs/Crc/crc16_mcrf4xx/build$ /home/xtwh/progs/Crc/crc16_mcrf4xx/build/crc16_mcrf4xx
// 15.0234 ms      15023388 count  0
// 15.6979 ms      15697930 count  0
// 18.1251 ms      18125098 count  0
// 17.1236 ms      17123603 count  0
// 18.4194 ms      18419387 count  0
// 43.0373 ms      43037268 count  34464
// 6005.05 ms      6005046863 count        50336
// 6028.01 ms      6028005272 count        50336
// 5894.55 ms      5894547652 count        50336
// xtwh@u2004:~/progs/Crc/crc16_mcrf4xx/build$ /home/xtwh/progs/Crc/crc16_mcrf4xx/build/crc16_mcrf4xx
// 15.7043 ms      15704331 count  0
// 16.098 ms       16097953 count  0
// 18.8471 ms      18847106 count  0
// 18.492 ms       18491960 count  0
// 20.6712 ms      20671192 count  0
// 48.2922 ms      48292169 count  34464
// 6286.35 ms      6286351188 count        50336
// 5929.94 ms      5929935406 count        50336
// 5607.64 ms      5607637688 count        50336
// xtwh@u2004:~/progs/Crc/crc16_mcrf4xx/build$ 